package com.flyqiu.flow.model.entity.hi;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.flyqiu.common.tools.LocalDateTimeTools;
import com.flyqiu.flow.common.std.InstUserTaskComplete;
import com.flyqiu.flow.model.entity.base.Task;
import com.flyqiu.flow.model.entity.ru.ProcRuUserTask;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.Duration;
import java.time.LocalDateTime;

@EqualsAndHashCode(callSuper = true)
@Data
@TableName(ProcHiUserTask.TABLE_NAME)
public class ProcHiUserTask extends Task implements InstUserTaskComplete {
    public static final String TABLE_NAME = "FLY_FLOW_HI_TASK";
    /**
     * 任务结束数据
     */
    @TableField("END_TIME_")
    private LocalDateTime endTime;
    /**
     * 任务耗时毫秒
     */
    @TableField("DURATION_")
    private Long duration;

    public static ProcHiUserTask createByUserTaskInst(ProcRuUserTask taskInst) {
        ProcHiUserTask result = new ProcHiUserTask();
        result.setId(taskInst.getId());
        result.setParentTaskId(taskInst.getParentTaskId());
        result.setProcInstId(taskInst.getProcInstId());
        result.setProcDeployDefKey(taskInst.getProcDeployDefKey());
        result.setProcDeployDefId(taskInst.getProcDeployDefId());
        result.setTaskDefKey(taskInst.getTaskDefKey());
        result.setTaskDefName(taskInst.getTaskDefName());
        result.setActId(taskInst.getActId());

        result.setAssignee(taskInst.getAssignee());
        result.setAssigneeKey(taskInst.getAssigneeKey());
        result.setAssigneeUniqueKey(taskInst.getAssigneeUniqueKey());
        result.setAssigneeAttr(taskInst.getAssigneeAttr());
        result.setAssigneeNickName(taskInst.getAssigneeNickName());

        result.setOwner(taskInst.getOwner());
        result.setOwnerKey(taskInst.getOwnerKey());
        result.setOwnerUniqueKey(taskInst.getAssigneeUniqueKey());
        result.setOwnerAttr(taskInst.getAssigneeAttr());
        result.setOwnerNickName(taskInst.getAssigneeNickName());

        result.setTenantId(taskInst.getTenantId());
        result.setCreateTime(taskInst.getCreateTime());
        return result;
    }


    public static ProcHiUserTask createByUserTaskInst(ProcRuUserTask taskInst, LocalDateTime endTime) {
        ProcHiUserTask result = createByUserTaskInst(taskInst);
        if (endTime == null) return result;
        result.setEndTime(endTime);
        result.duration = Duration.between(result.getCreateTime(), result.getEndTime()).toMillis();
        return result;
    }

    @Override
    public void completeAssignee(String assignee, String assigneeKey) {
        setAssignee(assignee);
        if (assigneeKey == null) {
            assigneeKey = "";
        }
        setAssigneeKey(assigneeKey);
    }

    @Override
    public void completeEndTime(LocalDateTime endTime) {
        setEndTime(endTime);
        duration = LocalDateTimeTools.timeDiffMillis(getCreateTime(), endTime);
    }


    @Override
    public void completeOwner(String owner, String ownerKey) {
        setOwner(owner);
        if (ownerKey == null) {
            ownerKey = "";
        }
        setOwnerKey(ownerKey);
    }
}
